<template>
  <div
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    style="marginLeft:20px;marginRight:20px"
  >
    <div>
      <el-input
        v-model="name"
        placeholder="请输入权限名"
        style="width:240px"
      ></el-input>
      <el-input
        v-model="key"
        placeholder="请输入权限密码"
        style="width:240px"
      ></el-input>
      <el-button type="primary" icon="el-icon-setting" @click="onsetting"
        >设置权限</el-button
      >
    </div>
    <div>
      <el-input
        v-model="content"
        type="textarea"
        placeholder="请输入文字"
        style="width:500px"
      ></el-input>
      <el-button
        class="fabutton"
        type="primary"
        icon="el-icon-position"
        @click="postlog"
        >发表日志</el-button
      >
    </div>
    <el-table :data="tableData" border style="width: 100%" stripe>
      <el-table-column
        prop="date"
        label="发表时间"
        width="200px"
      ></el-table-column>
      <el-table-column prop="content" label="内容"></el-table-column>
      <el-table-column label="操作" width="200px">
        <template slot-scope="scope">
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        @current-change="handleCurrentChange"
        layout="total ,prev, pager, next ,jumper"
        :total="count"
        :page-size="mount"
      ></el-pagination>
    </div>
  </div>
</template>
<script>
import { GetLog, DeleteLog, SetPermissins, PostLog } from "../../api/index"
export default {
  data() {
    return {
      loading: false,
      key: "",
      name: "",
      content: "", //日志的内容
      count: null, //总页数
      mount: 10, //每页展示数据的条数
      currentpage: 1,
      tableData: [],
    }
  },
  created() {
    this.getlog({ page: this.currentpage, limit: this.mount })
  },
  methods: {
    // 删除日志
    handleDelete(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          DeleteLog({ _id: row._id }).then((res) => {
            if (res.status) {
              this.$notify({
                title: "成功",
                message: res.msg,
                type: "success",
              })
              this.getlog({ page: this.currentpage, limit: this.mount })
            } else {
              this.$notify.error({
                title: "错误",
                message: res.msg,
              })
            }
          })
        })
        .catch(() => {
          this.$notify.info({
            title: "消息",
            message: "已取消删除操作",
          })
        })
    },

    // 当页码发生改变时
    handleCurrentChange(val) {
      this.currentpage = val
      this.getlog({ page: this.currentpage, limit: this.mount })
    },

    // 点击设置按钮时
    onsetting() {
      SetPermissins({ name: this.name, key: this.key }).then((res) => {
        if (res.status) {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
          })
          this.name = ""
          this.key = ""
        } else {
          this.$notify.error({
            title: "错误",
            message: res.msg,
          })
        }
      })
    },

    //获取日志的方法
    getlog(data) {
      GetLog(data).then((res) => {
        this.tableData = []
        if (res.status) {
          this.tableData = res.data
          this.count = res.count
        }
        this.loading = false
      })
    },
    postlog() {
      PostLog({ content: this.content }).then((res) => {
        if (res.status) {
          this.$notify({
            title: "成功",
            message: res.msg,
            type: "success",
          })
          this.getlog({ page: this.currentpage, limit: this.mount })
        } else {
          this.$notify.error({
            title: "错误",
            message: res.msg,
          })
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.pagination {
  margin: 10px 0px;
  text-align: right;
}

.el-input {
  margin-right: 20px;
  margin-bottom: 20px;
}
.el-select {
  width: 150px;
}
.el-textarea {
  margin-right: 20px;
}
.el-table {
  margin-top: 20px;
}
</style>
